<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

/**
 * @group 后台接口文档
 * @title 公共接口模块
 * @author chenke
 * @description 包含文件上传、图片处理等公共接口
 * */
class Base extends Controller
{

    /**
     * @title 单张图片上传
     * @description 后台用户列表数据
     * @url /admin/upimg
     * @method POST
     * @author chenke
     * @header name:dirpath type:string require:0 default:images desc:文件存储目录
     * @param name:images type:object require:1 default: other: desc:文件提交名
     * @return code:错误码
     * @return message:错误提示
     * @return data:图片的相对路径
     */
    public function uploadImage(Request $request){
        $rules = [
            'images'=>'required|mimes:jpeg,jpg,png,gif,ico,svg,bmp'
        ];
        $messages = [
            'images.required'=>'请上传图片',
            'images.image'=>'图片格式错误'
        ];
        $data = $request->all();
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $dirpath = $request->header('dirpath')?'images/'.$request->header('dirpath'):'images';
        $files = $request->file('images');//上传文件
        if(!$files->isValid()){
            return response()->json([
                'code'=>1,
                'message'=>'文件上传失败'
            ]);
        }
        $cansize = 2 * 1024 * 1024;
        $size = $files->getSize();
        if($size>$cansize){
            return response()->json([
                'code'=>1,
                'message'=>'图片大小不能超过10M'
            ]);
        }
        $filepath = $files->store($dirpath,'public');
        return response()->json([
            'code'=>0,
            'data'=>'/storage/'.$filepath
        ]);
    }

    /**
     * @title 多图片上传
     * @description 后台用户列表数据
     * @url /admin/upimgs
     * @method POST
     * @author chenke
     * @header name:dirpath type:string require:0 default:images desc:文件存储目录
     * @param name:images[] type:object require:1 default: other: desc:文件提交名
     * @return code:错误码
     * @return message:错误提示
     * @return data:所有图片的相对路径
     */
    public function mutiUploadImg(Request $request){
        if(!$request->hasFile('images')){
            return response()->json([
                'code'=>1,
                'message'=>'请上传图片'
            ]);
        }
        $extensions =['gif','png','jpg','jpeg','svg','ico','bmp']; //允许上传的图片类型
        $dirpath = $request->header('dirpath')?'images/'.$request->header('dirpath'):'images';
        $cansize = 10 * 1024 * 1024;
        $files = $request->file('images');//上传文件
        if(!is_array($files)){
            return response()->json([
                'code'=>1,
                'message'=>'图片数据格式错误'
            ]);
        }
        $urls = [];
        $errs = [];
        foreach ($files as $file){
            $ext = $file->extension();
            if(!in_array($ext,$extensions)){
                $errs = [
                    'code'=>1,
                    'message'=>'上传的图片类型不支持'
                ];
                break;
            }
            $size = $file->getSize();
            if($size>$cansize){
                $errs = [
                    'code'=>1,
                    'message'=>'图片大小不能超过10M'
                ];
                break;
            }
            $path = $file->store($dirpath,'public');
            $urls[] = '/storage/'.$path;
        }
        if(!empty($errs)){
            return response()->json($errs);
        }else{
            return response()->json([
                'code'=>0,
                'data'=>$urls
            ]);
        }
    }
}
